package com.xiaoyu.lanling.feature.user.model

import `in`.srain.cube.request.JsonData
import android.text.TextUtils
import com.xiaoyu.base.data.UserData
import com.xiaoyu.base.data.UserKVConfigData
import com.xiaoyu.lanling.event.vip.VipTypeUpdateEvent

class UserExtra {

    companion object {
        const val KEY_PREFERENCE = "preference"
        const val KEY_BASE_SETTING = "baseSetting"
        const val KEY_VERIFY = "verify"
        const val KEY_VIP_TYPE = "vipType"
        const val KEY_HIDE_DISTANCE = "hide_distance"
        const val KEY_HIDE_FOOT_PRINT = "hide_footprint"

        val instance by lazy { UserExtra() }
    }

    object VerifyStatus {
        const val UNVERIFIED = "unverified"
        const val VERIFYING = "verifying"
        const val SUCCESS = "success"
        const val FAILED = "failed"
    }

    var preference: UserPreference = UserPreference(JsonData.newMap())
        private set

    var baseSetting: UserBaseSetting = UserBaseSetting(JsonData.newMap())
        private set

    var verify: String = "unverified"
        private set

    var vipType: String = ""
        private set

    var hideDistance: Boolean = false
        private set

    var hideVisit: Boolean = false
        private set

    val isVip: Boolean
        get() {
            return !TextUtils.isEmpty(vipType)
        }

    fun restore(jsonData: JsonData) {
        preference = UserPreference(jsonData.optJson(KEY_PREFERENCE))
        baseSetting = UserBaseSetting(jsonData.optJson(KEY_BASE_SETTING))
        verify = jsonData.optString(KEY_VERIFY)
        vipType = jsonData.optString(KEY_VIP_TYPE)
        save()
    }

    fun updateAndSave(jsonData: JsonData) {
        preference = UserPreference(jsonData.optJson(KEY_PREFERENCE))
        baseSetting = UserBaseSetting(jsonData.optJson(KEY_BASE_SETTING))
        verify = jsonData.optString(KEY_VERIFY)

        val newVipType = jsonData.optString(KEY_VIP_TYPE)
        if (!TextUtils.equals(vipType, newVipType)) {
            VipTypeUpdateEvent(vipType, newVipType).post()
        }
        vipType = newVipType
        save()
    }

    fun updatePreference(sex: String, verify: String, minAge: String, maxAge: String, province: String, city: String) {
        preference.sex = sex
        preference.verify = verify
        preference.minAge = minAge
        preference.maxAge = maxAge
        preference.province = province
        preference.city = city
        save()
    }

    fun updateSetting(cupidAndAccostSwitch: Boolean) {
        baseSetting.cupidAndAccostSwitch = cupidAndAccostSwitch
        save()
    }

    fun updateOnTVSetting(showOnTV: Boolean) {
        baseSetting.showOnTV = showOnTV
        save()
    }

    fun updateVipType(type: String) {
        vipType = type
        save()
    }

    fun updatePrivilegeSetting(hideDistance: Boolean, hideFootPrint: Boolean) {
        this.hideDistance = hideDistance
        this.hideVisit = hideFootPrint
        save()
    }

    private fun toJson(): JsonData {
        val jsonData = JsonData.newMap()
        jsonData.put(KEY_PREFERENCE, preference.toJson())
        jsonData.put(KEY_BASE_SETTING, baseSetting.toJson())
        jsonData.put(KEY_VERIFY, verify)
        jsonData.put(KEY_HIDE_DISTANCE, hideDistance)
        jsonData.put(KEY_HIDE_FOOT_PRINT, hideVisit)
        return jsonData
    }

    private fun save() {
        UserData.getInstance().saveUserKV(UserKVConfigData.KEY_USER_EXTRA, toJson())
    }
}